import os

from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import User
import uuid

# Create your models here.
from django.http import Http404


def user_directory_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = '{}.{}'.format(uuid.uuid4().hex[:8], ext)
    if ext.lower() in ['jpg', 'jpeg', 'gif', 'png']:
        sub_folder = "avatar"
    elif ext.lower() in ['pdf', 'doc', 'docx', 'xls', 'xlsx']:
        sub_folder = 'document'
    else:
        return False
    return os.path.join(instance.user.id, sub_folder, filename)


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    avatar = models.ImageField(upload_to=user_directory_path, verbose_name="头像")
    files = models.FileField(upload_to='files', verbose_name="文件")